<?php
class signin extends CI_Controller {
	function __construct() {
		parent::__construct ();
		$this->load->helper ( 'url' );
		$this->load->helper('cookie');
		session_start();
	}
	public function index() {
		$lang ['error'] = '';
		$this->load->view ( 'public/v_signin', $lang );
	}
	
	//register
	public function regist(){
		if($this->vali_mail($this->input->post("usermail")))
		{
			$data["usermail"] = $this->input->post("usermail");
			$data["password"] = md5($this->input->post("password"));
			$uid = $this->m_tbk->add_user($data);
			if($uid){
				$_SESSION['uid'] = $uid;
				$_SESSION['usermail'] = $data['usermail'];
				$lang['error'] = $this->_lang['login_reg_success'];
				$this->load->view('public/v_into.php',$lang);
			}
			else{
				$lang['error'] = $this->_lang['login_is_reged'];
				$this->load->view('public/v_signin',$lang);
			}
		}
		else{
			$lang['error'] = $this->_lang['tbk_wrong_mail'];
			$this->load->view('public/v_signin',$lang);
		}
	}
	
	//validator
	private function vali_mail($mail)
	{
		if(preg_match('/^[A-Za-z0-9]+([\_?][A-Za-z0-9]+)*@[A-Za-z0-9]{2,16}\.com$/',$mail)){
			return true;
		}
		else{
			return false;
		}
	}
	
	public function into(){
		$lang ['error'] = '';
		$this->load->view ( 'public/v_into', $lang );
	}
	//validator login info
	public function into_veirtf(){
		foreach ($_POST as $key=>$v){
			$_POST[$key] = trim($v);
		}
		$_POST["password"] = md5($_POST["password"]);
		$us_re = $this->m_tbk->getOne("user",$_POST);
		if($us_re){
			$_SESSION["uid"] = $us_re["id"];
			$_SESSION["usermail"] = $us_re["usermail"];
			$cookie = array(
					'name'   => 'uid',
					'value'  => $us_re["id"],
					'expire' => '86500',    //24小时
			);
			$this->input->set_cookie($cookie);
			$cookie = array(
					'name'   => "usermail",
					'value'  => $us_re["usermail"],
					'expire' => '86500',    //24小时
			);
			$this->input->set_cookie($cookie);
			header("Location:".site_url());
		}else{
			$lang ['error'] = '';
			$lang ["waitSecond"] = 5;
			$lang ["jumpUrl"] = site_url("public/signin/into");
			$lang ["msg"] = "登入失败！请检查账号密码是否正确";
			$this->load->view ( 'public/v_error', $lang );
		}
	}
	//用户登出
	public function singnout(){
		session_destroy();
		$cookie = array(
				'name'   => 'uid',
				'value'  => '',
				'expire' => '86500',    //24小时
		);
		$this->input->set_cookie($cookie);
		$cookie = array(
				'name'   => "usermail",
				'value'  => '',
				'expire' => '86500',    //24小时
		);
		$this->input->set_cookie($cookie);
		delete_cookie();
		header("Location:".site_url("tbk"));
	}
}